package com.itheima.reggie.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.dto.PageDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {

        return categoryService.add(category);
    }

    /**
     * 分页查询菜品分类
     */
    @GetMapping("/page")
    public R page(PageDto dto) {
        /*dto.check();
        return R.success(categoryService.page(new Page<>(dto.getPage(), dto.getPageSize())));*/
        return categoryService.pageInfo(dto);
    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R removeById(Long id) {

        return categoryService.removeById(id);
    }

    /**
     * 修改分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    public R updatecate(@RequestBody Category category) {

        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 查询所有的分类（菜品添加的时候使用）
     */
    @GetMapping("/list")
    public R findList(Integer type) {
        if (ObjectUtil.isNotNull(type) && type <= 0) type = 1;
        return R.success(categoryService.list(Wrappers.lambdaQuery(Category.class).eq(ObjectUtil.isNotNull(type), Category::getType, type)));
    }


}
